package com.banciyuan.bcywebview.biz.detail.complex.holder;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/8/10.
 */
public class ChargeHolder extends RecyclerView.ViewHolder {

    private TextView chargeIntro_tv;
    private TextView chargeLargeCount;
    private TextView chargeSmallCount;
    private View rank_rl;
    private ImageView[] user_iv;
    private LinearLayout charge_line;
    private TextView time_tv;
    private View split_line;

    public ChargeHolder(View itemView) {
        super(itemView);
        charge_line = (LinearLayout) itemView.findViewById(R.id.charge_container);
        chargeIntro_tv = (TextView) itemView.findViewById(R.id.charge_text);
        chargeLargeCount = (TextView) itemView.findViewById(R.id.charge_large_count);
        chargeSmallCount = (TextView) itemView.findViewById(R.id.charge_small_count);
        rank_rl = itemView.findViewById(R.id.rank_line);
        user_iv = new ImageView[]{(ImageView) itemView.findViewById(R.id.user_head_one),
                (ImageView) itemView.findViewById(R.id.user_head_two),
                (ImageView) itemView.findViewById(R.id.user_head_three)};
        time_tv = (TextView) itemView.findViewById(R.id.detail_time);
        split_line = itemView.findViewById(R.id.split_line);
    }

    public void setData(final Complex item, final Context mContext) {
        if (item.getTimelineType() == TimelineFactory.TIMELINE_TYPE_DAILY && StringUtil.notNullEqual("svideo", item.getType())) {
            split_line.setVisibility(View.VISIBLE);
        } else {
            split_line.setVisibility(View.GONE);
        }
        if (item.getTimelineType() == TimelineFactory.TIMELINE_TYPE_WRITER
                || (item.getTimelineType() == TimelineFactory.TIMELINE_TYPE_DAILY && StringUtil.notNullEqual(item.getType(), "svideo"))) {
            time_tv.setVisibility(View.GONE);
        } else {
            if (!TextUtils.isEmpty(item.getCtime())) {
                time_tv.setVisibility(View.VISIBLE);
                time_tv.setText(String.format(mContext.getString(R.string.post_at), StringUtil.getOppositeTime(item.getCtime())));
            }
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) time_tv.getLayoutParams();
            params.width = CommonUtils.getWidthPixels((Activity) mContext);
            time_tv.setLayoutParams(params);
        }
        if (item.getPayment() != null) {
            if (item.getPayment().getStatus().equals("1")) {
                charge_line.setVisibility(View.VISIBLE);
                charge_line.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BaseObserver.getInstance().notifyWatchers(BaseObserver.CHARGE_EVENT);
                    }
                });

                if (!TextUtils.isEmpty(item.getPayment().getIntro())) {
                    chargeIntro_tv.setText(String.format(mContext.getString(R.string.chargetext_format), Html.fromHtml(item.getPayment().getIntro())));
                    chargeIntro_tv.setVisibility(View.VISIBLE);
                } else {
                    chargeIntro_tv.setVisibility(View.GONE);
                }
                int user_count = 0;
                try {
                    user_count = Integer.valueOf(item.getPayment().getUser_count());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (user_count <= 0) {
                    rank_rl.setVisibility(View.GONE);
                } else {
                    rank_rl.setVisibility(View.VISIBLE);
                    if (user_count <= 3) {
                        chargeSmallCount.setText(Html.fromHtml(String.format(mContext.getString(R.string.charge_count), user_count)));
                        chargeSmallCount.setVisibility(View.VISIBLE);
                        chargeLargeCount.setVisibility(View.GONE);
                    } else {
                        chargeLargeCount.setText(String.format(mContext.getString(R.string.chargenum_format), user_count));
                        chargeSmallCount.setVisibility(View.GONE);
                        chargeLargeCount.setVisibility(View.VISIBLE);
                    }

                    List<UserDetail> ulist = item.getPayment().getTop_users();
                    if (ulist != null && ulist.size() > 0) {
                        rank_rl.setVisibility(View.VISIBLE);
                        rank_rl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                BaseObserver.getInstance().notifyWatchers(BaseObserver.CHARGE_RANK);
                            }
                        });
                    }
                    for (int i = 0; i < user_iv.length; i++) {
                        if (null != ulist && i < ulist.size()) {
                            user_iv[i].setVisibility(View.VISIBLE);
                            ImageLoader.getInstance().displayImage(ulist.get(i).getAvatar(), user_iv[i], ImageOptions.optionHead);
                        } else {
                            user_iv[i].setVisibility(View.GONE);
                        }
                    }
                }

            } else {
                charge_line.setVisibility(View.GONE);
                split_line.setVisibility(View.GONE);
            }
        } else {
            charge_line.setVisibility(View.GONE);
            split_line.setVisibility(View.GONE);
        }
    }
}
